//
//  HNHappyFamilyJJGameController.m
//  XDEducation
//
//  Created by 星道 on 17/6/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyFamilyJJGameController.h"
#import "AppDelegate.h"
#import <WebKit/WebKit.h>

@interface HNHappyFamilyJJGameController ()<WKNavigationDelegate>
@property (nonatomic, strong) AppDelegate *Appdelegate;
//网络获取到的可玩游戏时长
@property (nonatomic, assign) NSInteger PlayGameTime;
//游戏计时时间
@property (nonatomic, assign) NSInteger PlayTime;
@end

@implementation HNHappyFamilyJJGameController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.title = @"JJ小游戏";

    if ([[AccountTool shareAccountTool].currentAccount.rolecode isEqualToString:@"4"])
    {
        [self AlertShow:@"该功能尚处于调试阶，孩子角色进入jj小游戏限制20秒" Message:@"安卓端游戏时长暂时无法上传，现无法将已进行时长的数据传递" handler:^(UIAlertAction *action) {
            //先获取游戏
            [self RefreshData];
        }];
    }
    else
    {
        [self RefreshDataWithBeginPlayGame];
    }
}



#pragma mark - 网络请求
/** 获取玩游戏的时间 */
- (void)RefreshData
{
    [MBProgressHUD showHUD];
    NSDictionary *Params = @{
                             @"action":@"getTime2",
                             @"uid"   :[AccountTool shareAccountTool].currentAccount.uid,
                             @"type"  :@2
                             };
    [XDHttpTool SessGet:BaseURL(@"yipark.do?") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {

        [MBProgressHUD hideHUD];
        if (responseObj)
        {
            NSString *Str = [[NSString alloc] initWithData:responseObj encoding:NSUTF8StringEncoding];
            Str = @"0.33";
            self.PlayGameTime = [Str floatValue] * 60;
            if (self.PlayGameTime == -1)
            {
                [MBProgressHUD showError:@"您当前不在设定的时间段内"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else if (self.PlayGameTime == 0)
            {
                [MBProgressHUD showError:@"您当前没有剩余的游戏时间了"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                //做个提示之后再进浏览
                [self AlertShow:@"温馨提示" Message:[NSString stringWithFormat:@"您当前可娱乐时间为%.ld秒",self.PlayGameTime] handler:^(UIAlertAction *action) {

                    //开始玩游戏
                    [self RefreshDataWithBeginPlayGame];
                }];
            }
        }
        else
        {
            [MBProgressHUD showError:responseObj[@"msg"]];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
    }];
}


#pragma mark - webView
/** 游戏时间获取后开始访问游戏页面并开始计时 */
- (void)RefreshDataWithBeginPlayGame
{
    WKWebView *WebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
    NSURLRequest *Request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wan.jj.cn/?f=1047"]];
    WebView.navigationDelegate = self;
    [WebView loadRequest:Request];
    [self.view addSubview:WebView];

    [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(PlayGameTime:) userInfo:nil repeats:YES];
}

//玩游戏的时长
- (void)PlayGameTime:(NSTimer *)Timer
{
            self.PlayTime += 1;
            //如果游戏时长等于对象的可玩的时间，说明到达限制的时间
            if (self.PlayTime == self.PlayGameTime)
            {
                //停止计时
                [Timer invalidate];
                //已看时长清零
                self.PlayTime = 0;
                //提示
                [self AlertShow:@"温馨提示" Message:@"您的游戏时长已用完,即将关闭游戏界面                                                                            " handler:^(UIAlertAction *action) {
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }

}





#pragma mark - Function
/** AlertController */
- (void)AlertShow:(NSString *)Title Message:(NSString *)Message handler:(void (^ __nullable)(UIAlertAction *action))handler
{
    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:Title message:Message preferredStyle:(UIAlertControllerStyleAlert)];
    [Alert addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDestructive) handler:handler]];
    [self presentViewController:Alert animated:YES completion:nil];
}


@end

